package com.itic.web.company.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.demo.modelfiles.persistence.model.ModelFiles;
import com.itic.demo.modelfiles.service.ModelFilesService;
import com.itic.ht.actcms.actions.persistence.model.ActionModel;
import com.itic.ht.actcms.actions.persistence.model.ActionModelExample;
import com.itic.ht.actcms.actions.service.ActionModelService;
import com.itic.ht.actcms.notice.persistence.model.WebNotice;
import com.itic.ht.actcms.notice.persistence.model.WebNoticeExample;
import com.itic.ht.actcms.notice.service.WebNoticeService;
import com.itic.ht.mem.member.persistence.model.WebMember;
import com.itic.ht.mem.member.service.WebMemberService;
import com.itic.web.qo.TokenData;
import com.itic.web.ro.ResResult;
import com.itic.web.util.TokenUtil;

@Service
@Transactional
public class CompanyService {
    @Autowired
    private WebNoticeService noticeService;
    @Autowired
    private ActionModelService actionModelService;
	@Autowired
	private WebMemberService webMemberService;
    @Autowired
    private ModelFilesService modelFilesService;
    
	/**
	 *  验证token
	 *  (上级存在   而且自己的pid为空时)         关联用户关系
	 */
	public ResResult getIndex(TokenData data,String noticeType){
		
		List<WebNotice> list = new ArrayList<WebNotice>();
		WebMember member = null;
		try {
			//判断token是不是存在
			if(StringHelper.isEmpty(data.getToken())){
				return ResResult.error(1, "token不能为空");
			}
			//先判断token正确性
			boolean flag = TokenUtil.assertUserToken(data);
			if(!flag){
				return ResResult.error(2, "token失效");
			}
			
			//从token中取account信息   
			data.setAccount(TokenUtil.getUserList(data.getToken()).getAccount().trim());
			//取得用户信息
			member = webMemberService.getMemberByAccount(data.getAccount());
			System.out.println("现在要查询的帐号是:"+member.getAccount());
        

			WebNoticeExample example = new WebNoticeExample();
			example.createCriteria().andCategoryEqualTo(noticeType).andIsEffectiveEqualTo(DBConst.TRUE);
			example.setOrderByClause("REORDER ASC");
	        
			list = noticeService.list(example);
			if(list!=null && list.size()>0){
                 for (WebNotice notice : list) {
 					notice.setCreateTime(null);
 					notice.setUpdateTime(null);
 					notice.setCategory(null);
 					notice.setAuthor(null);
 					notice.setOrgCode(null);
 					notice.setCreateUser(null);
 					
                 	String fileId = notice.getFileId();
                 	if(fileId!=null && !"".equals(fileId)){
                 		ModelFiles file = modelFilesService.getFile(fileId);
                 		     if(file!=null){
                 		    	notice.setFileId(file.getFilePath());
                 		     }
                 	}
			     }
			}else{
				list = new ArrayList<WebNotice>();
			}
            
			return ResResult.build(200, "查询成功",list);
		} catch (Exception e) {
			//返回错误代码
			e.printStackTrace();
			return ResResult.error(4, "查询失败");
		}
	}
	/**
	 *  验证token
	 *  (上级存在   而且自己的pid为空时)         关联用户关系
	 */
	public ResResult getActions(TokenData data){
		
		List<ActionModel> list = new ArrayList<ActionModel>();
		WebMember member = null;
		try {
			//判断token是不是存在
			if(StringHelper.isEmpty(data.getToken())){
				return ResResult.error(1, "token不能为空");
			}
			//先判断token正确性
			boolean flag = TokenUtil.assertUserToken(data);
			if(!flag){
				return ResResult.error(2, "token失效");
			}
			
			//从token中取account信息   
			data.setAccount(TokenUtil.getUserList(data.getToken()).getAccount().trim());
			//取得用户信息
			member = webMemberService.getMemberByAccount(data.getAccount());
			System.out.println("现在要查询的帐号是:"+member.getAccount());
			
			//ActionModelService
			ActionModelExample example = new ActionModelExample();
			example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE);
			example.setOrderByClause("REORDER ASC");
			
			list = actionModelService.list(example);
			if(list!=null && list.size()>0){
				for (ActionModel notice : list) {
					notice.setCreateTime(null);
					notice.setUpdateTime(null);
					notice.setCategory(null);
 					notice.setOrgCode(null);
 					notice.setCreateUser(null);
 					
					String fileId = notice.getFileId();
					if(fileId!=null && !"".equals(fileId)){
						ModelFiles file = modelFilesService.getFile(fileId);
						if(file!=null){
							notice.setFileId(file.getFilePath());
						}
					}
				}
			}else{
				list = new ArrayList<ActionModel>();
			}
			
			return ResResult.build(200, "查询成功",list);
		} catch (Exception e) {
			//返回错误代码
			e.printStackTrace();
			return ResResult.error(4, "查询失败");
		}
	}
}
